package com.me.study.app.general

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Rect
import android.util.AttributeSet
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import com.blankj.utilcode.util.ScreenUtils
import com.me.study.R


/**
 *create author : FZW
 *create date  : 2024/7/5
 */
class LaunchView(context: Context, attrs: AttributeSet) :
    LinearLayout(context, attrs) {

    private var screenWidth = ScreenUtils.getScreenWidth()
    private var screenHeight = ScreenUtils.getScreenHeight()

    init {
        screenWidth = ScreenUtils.getScreenWidth()
        screenHeight = ScreenUtils.getScreenHeight()
        initBitmap()
        orientation = VERTICAL
    }

    interface OnAnimationListener {
        fun onAnimationEnd(isEnter: Boolean)
    }

    private var mBitmap: Bitmap? = null
    private var croppedBitmapTop: Bitmap? = null
    private var croppedBitmapBtm: Bitmap? = null
    private var topAnimationEnd: Boolean = false
    private var btmAnimationEnd: Boolean = false
    private var topAnimationStart: Boolean = false
    private var btmAnimationStart: Boolean = false
    private var isAnimationEnter: Boolean = true
    private var mAnimationListener: OnAnimationListener? = null

    private fun initBitmap() {
        mBitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_initiation_bg)

        mBitmap?.let {
            Bitmap.createScaledBitmap(it, screenWidth, screenHeight, true)?.let {
                croppedBitmapTop = cropImage(it, 0, 0, screenWidth, screenHeight / 2)
                croppedBitmapBtm = cropImage(it, 0, screenHeight / 2, screenWidth, screenHeight / 2)
                croppedBitmapTop?.let {
                    val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
                    val imag = ImageView(context)
                    imag.layoutParams = params
                    imag.setImageBitmap(it)
                    addView(imag)
                }
                croppedBitmapBtm?.let {
                    val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
                    val imag = ImageView(context)
                    imag.layoutParams = params
                    imag.setImageBitmap(it)
                    addView(imag)
                }
            }

        }
    }

    private fun cropImage(srcBitmap: Bitmap, x: Int, y: Int, width: Int, height: Int): Bitmap {
        // 创建一个新的Bitmap，其大小取决于裁剪区域的大小
        val croppedBitmap = Bitmap.createBitmap(width, height, srcBitmap.config)
        // 创建一个Canvas，在新的Bitmap上绘制
        val canvas = Canvas(croppedBitmap)
        // 创建一个Rect，定义了要裁剪的区域
        val rect = Rect(x, y, x + width, y + height)
        // 绘制裁剪区域
        canvas.drawBitmap(srcBitmap, rect, Rect(0, 0, width, height), null)
        return croppedBitmap
    }

    fun setOnAnimationListener(listener: OnAnimationListener) {
        mAnimationListener = listener
    }

    fun startAnimator() {
        if (topAnimationStart) {
            return
        }
        topAnimationStart = true
        isAnimationEnter = true
        val topView = getChildAt(0)
        if (topView is ImageView) {
            topAnimationEnd = false
            val animation = AnimationUtils.loadAnimation(context, R.anim.view_anim_enter_top)
            animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(p0: Animation?) {

                }

                override fun onAnimationEnd(p0: Animation?) {
                    topAnimationEnd = true
                    onListener()
                }

                override fun onAnimationRepeat(p0: Animation?) {

                }

            })
            topView.startAnimation(animation)
        }
        val btmView = getChildAt(1)
        if (btmView is ImageView) {
            btmAnimationEnd = false
            val animation = AnimationUtils.loadAnimation(context, R.anim.view_anim_enter_bom)
            animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(p0: Animation?) {

                }

                override fun onAnimationEnd(p0: Animation?) {
                    btmAnimationEnd = true
                    onListener()
                }

                override fun onAnimationRepeat(p0: Animation?) {

                }

            })
            btmView.startAnimation(animation)
        }
    }

    fun endAnimator() {
        if (btmAnimationStart) {
            return
        }
        btmAnimationStart = true
        isAnimationEnter = false
        val topView = getChildAt(0)
        if (topView is ImageView) {
            topAnimationEnd = false
            val animation = AnimationUtils.loadAnimation(context, R.anim.view_anim_out_top)
            animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(p0: Animation?) {

                }

                override fun onAnimationEnd(p0: Animation?) {
                    topView.visibility = GONE
                    topAnimationEnd = true
                    onListener()
                }

                override fun onAnimationRepeat(p0: Animation?) {

                }

            })
            topView.startAnimation(animation)
        }
        val btmView = getChildAt(1)
        if (btmView is ImageView) {
            btmAnimationEnd = false
            val animation = AnimationUtils.loadAnimation(context, R.anim.view_anim_out_bom)
            animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(p0: Animation?) {

                }

                override fun onAnimationEnd(p0: Animation?) {
                    btmView.visibility = GONE
                    btmAnimationEnd = true
                    onListener()
                }

                override fun onAnimationRepeat(p0: Animation?) {

                }

            })
            btmView.startAnimation(animation)
        }
    }

    private fun onListener() {
        if (topAnimationEnd && btmAnimationEnd) {
            when (isAnimationEnter) {
                true -> {
                    topAnimationStart = true
                }
                false -> {
                    btmAnimationStart = true
                }
            }
            mAnimationListener?.onAnimationEnd(isAnimationEnter)
        }

    }

}